Nginx Port and Domain Binding: Easily Achieve Domain Access to the Server

This article explains how to bind ports and domains in Nginx to achieve hosting multiple websites/services on a single server. The core is to distinguish different sites by "port + domain name". Nginx configures virtual hosts through the `server` block, with key directives including `listen` (port), `server_name` (domain name), `root` (file path), and `index` (home page). Prerequisites: The server needs Nginx installed, the domain name should be registered and resolved to a public IP, and the server should be tested to be accessible. Practical cases are divided into two scenarios: 1. The same domain name with different ports (e.g., binding 80 and 443 ports for `www.myblog.com`, with HTTPS certificate required for the latter); 2. Different domain names with different ports (e.g., `www.myblog.com` using port 80, `blog.myblog.com` using port 8080). Configuration files are stored in `/etc/nginx/conf.d/`, and examples should include `listen` and `server_name`. Verification: Execute `nginx -t` to check syntax, use `systemctl restart nginx` to apply changes, and verify access via a browser. Common issues: Configuration errors (check syntax), unapplied domain resolution (wait for DNS or use `nslookup`), port conflicts (change port or ...).

Read More